<template>
  <VPage class="app-container">
    <VTable ref="tableRef" v-bind="tableOptins">
      <template #form="{ form }">
        <multiInput class="w-50" v-model="form.sku" placeholder="SKU/自定义SKU"></multiInput>
        <multiInput class="w-50" v-model="form.tracking_number" placeholder="快递单号"></multiInput>
        <multiInput class="w-50" v-model="form.receipt_number" placeholder="采购单号"></multiInput>
        <SelectWarehouse class="selectClass" v-model="form.warehouse_id" placeholder="仓库" :multiple="true">
        </SelectWarehouse>
        <el-select class="selectClass" v-model="form.urgent_status" placeholder="是否加急" clearable>
          <el-option v-for="item in urgentOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
        </el-select>
        <el-select class="selectClass" v-model="form.qc_status" placeholder="质检状态" clearable>
          <el-option v-for="item in options['stockin.qcOrder']?.[0].orderQcStatus" :key="item.value" :label="item.label"
            :value="item.value"></el-option>
        </el-select>
        <VGroup>
          <el-select class="selectClass" v-model="form.timeRange.type" style="max-width: 150px">
            <el-option v-for="item in timeOption" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-date-picker v-model="form.timeRange.value" type="daterange" range-separator="至" start-placeholder="开始时间"
            end-placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" :default-time="defaultTime()"
            style="max-width: 300px" />
        </VGroup>
      </template>
      <template #toolbar_btns>
        <el-button type="primary" @click="handleQuality()">质检操作</el-button>
        <el-button class="ml-auto" @click="handleExport()"><svg-icon icon-class="import" />导出</el-button>
      </template>
    </VTable>
    <ComModal ref="ComModalRef" :options="options" @query="query"></ComModal>
    <derived-center ref="DerivedCenterRef"></derived-center>
  </VPage>
</template>
<script setup name="StorageQc-order" lang="jsx">
import { ElMessage } from 'element-plus'
import { ref, reactive, toRefs } from 'vue'
import { getEnums } from '@/api/public'
import { queryListOrder, reInspection } from './api'
import { getWareData } from '@/api/public'
import ComModal from './components/ComModal.vue'
import multiInput from '@/components/multiInput';
import SelectWarehouse from '@/components/SelectWarehouse'
import { useRouter, useRoute } from 'vue-router';
const router = useRouter()

const timeOption = [
  { label: "创建时间", value: "create_time" },
  { label: "质检时间", value: "qc_end_time" },
]

const urgentOptions = [
  {
    label: '是',
    value: 1
  },
  {
    label: '否',
    value: 0
  }
]

const tableRef = ref()
const tableOptins = reactive({
  rowConfig: { height: 'auto' },
  showHeaderOverflow: false,
  showOverflow: false,
  formConfig: {
    data: {
      timeRange: { type: 'create_time', value: [], range: true },
    }
  },
  columns: [
    {
      type: "seq", title: '序号', width: 80
    },
    {
      field: 'qc_order_no', title: '单号', width: 200, slots: {
        default: ({ row }) => {
          return <div class="base-text-color cursor-pointer" onClick={() => showOrderDetail(row.id)}> {row.qc_order_no} </div>
        }
      }
    },
    {
      field: 'receipt_number', title: '采购单号', width: 200
    },
    {
      field: 'express_json', title: '快递单号', width: 180
    },
    {
      field: 'qc_status_cn', title: '质检状态', width: 120
    },
    {
      field: 'create_time', title: '创建时间', width: 180
    },
    {
      field: 'receive_user', title: '点数人', width: 160
    },
    {
      field: 'receive_time', title: '点数时间', width: 160
    },
    {
      field: 'platform_order_no', title: '平台单号', width: 120
    },
    {
      field: 'business_type_cn', title: '收货类型', width: 120
    },
    {
      field: 'qc_end_time', title: '质检完成时间', width: 180
    },
    {
      field: 'warehouse_name', title: '收货仓', width: 120
    },
  ],
  proxyConfig: {
    seq: true,
    props: {
      result: 'data',
      total: 'total',
    },
    ajax: {
      query: async ({ page, form }) => {
        const res = await queryListOrder({ ...form, ...page })
        return new Promise(function (resolve) {
          resolve({
            data: res.data,
            total: res.total
          })
        })
      }
    }
  }
})
const query = () => {
  tableRef.value.query()
}

const data = reactive({
  options: [],
  wareDataList: [],
})
const { options, wareDataList } = toRefs(data)

// 获取枚举类
const fetchOption = async () => {
  getEnums({
    version: "v1", "name": [
      "stockin.qcOrder"
    ]
  }).then(res => {
    options.value = res.data
  })
}
fetchOption()

// 获取仓库
const getWareDataList = async () => {
  try {
    const res = await getWareData()
    wareDataList.value = res.data
  } catch (e) {
    throw Error(e)
  }
}
provide('wareDataList', wareDataList)
getWareDataList()


const logRef = ref()
const handleLog = (id) => {
  logRef.value.open(id)
}

const handleQuality = (row) => {
  router.push({
    path: '/storage/quality-testing',
    query: {
      // sku: row.sku,
      // qc_order_id: row.qc_order_id
    }
  })
}

const handleAagin = async (id) => {
  try {
    const res = await reInspection({
      detail_id: id
    })
    ElMessage.success(res.data.message)
    query()
  } catch (e) {
    throw Error(e)
  }
}

const DialogImageRef = ref()
const handleViewImage = () => {
  type.value == 'qc-factory'
  DialogImageRef.value.open(type.value == 'qc-factory' ? 4 : 3, detail_id.value)
}

const DerivedCenterRef = ref()
const handleExport = (val) => {
  let condition = tableRef.value.getQueryForm()
  DerivedCenterRef.value.open({
    id: 58,
    tag_name: 'wms',
    condition
  })
}

// 加工单详情
const ComModalRef = ref()

const showOrderDetail = (id) => {
  ComModalRef.value.open(id)
}
</script>